<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3.App组件的使用</title>
    <!-- 引入Vue -->
    <script type="text/javascript" src="../js/vue.js"></script>
</head>

<body>
    <!-- 准备好一个容器-->
    <div id="demo">

    </div>
</body>

<script type="text/javascript">
    //阻止 vue 在启动时生成生产提示。
    Vue.config.productionTip = false
    //创建一个Hello组件
    const Hello = Vue.extend({
        template: `<h2>Hello</h2>`
    })
    Vue.component('Hello', Hello)
    //创建School组件
    const School = Vue.extend({
        template: `
            <div>
                <Hello></Hello>
                <h2>学校名称:{{sname}}</h2>
                <h2>学校地址:{{address}}</h2>
                <button @click = "sname +='!'">改学校名</button>
                <button @click ="welcome">欢迎你</button>
            </div>`,
        data() {
            return {
                sname: '尚硅谷',
                address: '宏福科技园'
            }
        },
        methods: {
            welcome() {
                alert(`${this.sname}欢迎你!`)
            }
        },

    })
    //创建Person 组件
    const Person = Vue.extend({
        template: `
        <div>
            <Hello></Hello>
            <h2>人名:{{pname}}</h2>
            <h2>年龄:{{age}}</h2>
            <button @click = "age++">年龄+1</button>
            <button @click = "speak">说话</button>
        </div>
        `,
        data() {
            return {
                pname: '王五',
                age: 18
            }
        },
        methods: {
            speak() {
                alert('十一放假,必须嗨起来!!')
            }
        }
    })
    //所有组件的外壳组件
    const App = Vue.extend({
        components: {
            School,
            Person
        },
        template: `
        <div>
            <School></School>
            <hr>
            <Person></Person>
        </div>
        `,
    })
    new Vue({
        el: '#demo',
        components: {
            App
        },
        template: `<App></App>`
    })
</script>

</html>